package com.weiyan.blog.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;

/**
 * @author misterWei
 * @create 2020年01月20号:20点31分
 * @mailbox mynameisweiyan@gmail.com
 *
 * 资源控制管理器
 */
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter{
    private static final String RESOURCE_ID="rid"; //资源列表标识符号,必须和认证管理器 中保持一致


    @Autowired
    private TokenStore tokenStore;

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.resourceId(RESOURCE_ID)
                  .tokenStore(tokenStore)
                   .stateless(true); //无状态开启
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
       http.authorizeRequests()
                .antMatchers("/**").access("#oauth2.hasAnyScope('read','write')")
                  .and().csrf().disable()
                    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }
/*
    //资源服务令牌解析
    @Bean
    public ResourceServerTokenServices tokenServices(){
        RemoteTokenServices remoteTokenServices = new RemoteTokenServices();
        remoteTokenServices.setCheckTokenEndpointUrl("http://127.0.0.1:9020/oauth/check_token");
        remoteTokenServices.setClientId("cid");
        remoteTokenServices.setClientSecret("secret");
        return remoteTokenServices;
    }*/
}
